package com.shopping.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shopping.po.Admin;
import com.shopping.service.AdminService;

@Controller
public class AdminController {
	@Autowired
	private AdminService adminService;
	
	@RequestMapping("/adminlogin")
	public String toadminlogin() {
		return "admin";
	}
	
	
	@RequestMapping("/toadminlogin")
	public String check2(HttpServletRequest request,HttpSession session) {
		String adminname = request.getParameter("adminname");
		String password = request.getParameter("password");
		Admin admin1 = adminService.findadminByName(adminname);
		
		if(admin1 != null) {
			if(admin1.getPassword().equals(password)) {
				session.setAttribute("admin", admin1);
				return "redirect:/allStock";
			}else {
				request.setAttribute("password", "密码错误");
				return "admin";
			}
		}else {
			request.setAttribute("adminname", "您不是我们的管理员");
			return "admin";
		}
	}
	
	
	@RequestMapping("/tologout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/adminlogin";	
	}
}
